|
1
|
|
|
import path from 'path' |
|
2
|
|
|
import { |
|
3
|
|
|
coreUtils, |
|
4
|
|
|
cmsTemplates, |
|
5
|
|
|
cmsOperations, |
|
6
|
|
|
config, |
|
7
|
|
|
abeExtend, |
|
8
|
|
|
cmsData, |
|
9
|
|
|
Manager |
|
10
|
|
|
} from '../../' |
|
11
|
|
|
|
|
12
|
|
|
const create = function(templateId, pathCreate, name, req, forceJson = {}, duplicate = false) { |
|
13
|
|
|
const p = new Promise((resolve, reject) => { |
|
14
|
|
|
abeExtend.hooks.instance.trigger('beforeCreate', templateId, pathCreate, name, req, forceJson) |
|
15
|
|
|
|
|
16
|
|
|
const templatePath = path.join(config.root, config.templates.url, `${templateId}.${config.files.templates.extension}`) |
|
17
|
|
|
let postDataPath = path.join(pathCreate, name) |
|
18
|
|
|
postDataPath = coreUtils.slug.clean(postDataPath) |
|
19
|
|
|
postDataPath = path.join(config.root, config.data.url, postDataPath) |
|
20
|
|
|
|
|
21
|
|
|
if(templatePath !== null && postDataPath !== null) { |
|
22
|
|
|
if(!coreUtils.file.exist(postDataPath)) { |
|
23
|
|
|
let postData = (forceJson) ? forceJson : {} |
|
24
|
|
|
let template = cmsTemplates.template.getTemplate(templatePath) |
|
25
|
|
|
if (duplicate) { |
|
26
|
|
|
postData = cmsData.values.removeDuplicate(template, postData) |
|
27
|
|
|
} |
|
28
|
|
|
template = cmsData.source.removeDataList(template) |
|
29
|
|
|
var resHook = abeExtend.hooks.instance.trigger('beforeFirstSave', postDataPath, req.query, postData, template) |
|
30
|
|
|
postDataPath = resHook.filePath |
|
31
|
|
|
postData = resHook.json |
|
32
|
|
|
template = resHook.text |
|
33
|
|
|
|
|
34
|
|
|
abeExtend.hooks.instance.trigger('afterCreate', postData, template, pathCreate, name, req, forceJson) |
|
35
|
|
|
cmsOperations.save.save(postDataPath, templateId, postData, template, 'draft', null, 'draft') |
|
36
|
|
|
.then((resSave) => { |
|
37
|
|
|
Manager.instance.updatePostInList(resSave.jsonPath) |
|
38
|
|
|
resolve(resSave.json) |
|
39
|
|
|
}).catch(function(e) { |
|
40
|
|
|
reject() |
|
41
|
|
|
console.error(e) |
|
42
|
|
|
}) |
|
43
|
|
|
}else { |
|
44
|
|
|
postData = cmsData.file.get(postDataPath) |
|
|
|
|
|
|
45
|
|
|
resolve(postData, postDataPath) |
|
46
|
|
|
} |
|
47
|
|
|
}else { |
|
48
|
|
|
reject() |
|
49
|
|
|
} |
|
50
|
|
|
}).catch(function(e) { |
|
51
|
|
|
console.error(e) |
|
52
|
|
|
}) |
|
53
|
|
|
|
|
54
|
|
|
return p |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
export default create |